﻿
@{
    Layout = "~/Areas/Admin/Views/Shared/_Layout.cshtml";
}


<ol class="breadcrumb">
    <li><span class="glyphicon glyphicon-home"></span>  @Html.ActionLink(" 首页", "Index", "Home")</li>
    <li class="active">@Html.ActionLink("管理员", "Index", "Admin")</li>
</ol>

@section SideNav{@Html.Partial("SideNavPartialView")}

@Scripts.Render("~/bundles/jqueryval")
@Scripts.Render("~/bundles/bootstrapplugin")
@Styles.Render("~/Content/bootstrapplugincss")


<div id="toolbar"  class="btn-group" role="group">
    <button class="btn btn-default" id="btnAdd"><span class="glyphicon glyphicon-plus"></span> 添加</button>
    <button class="btn btn-default" id="btnDel"><span class="glyphicon glyphicon-remove"></span>删除</button>
</div>
<table id="admingrid"></table>


<script type="text/javascript">
    //重置密码
    function ResetPassword(id, account) {
        BootstrapDialog.confirm("确定重置账号 " + account + " 的密码?", function (result) {
            if (result) {
                $.post("@Url.Action("ResetPassword", "Admin")", { id: id }, function (data) {
                    if (data.Code == 1) {
                        BootstrapDialog.show({
                            title: "密码重置成功",
                            message: data.Message,
                            buttons: [{
                                icon: "glyphicon glyphicon-ok",
                                label: "确定",
                                action: function (dialogItself) {
                                    dialogItself.close();
                                }
                            }]
                        });
                    }
                    else {
                        BootstrapDialog.show({
                            title: "密码重置失败",
                            message: data.Message,
                            buttons: [{
                                icon: "glyphicon glyphicon-ok",
                                label: "确定",
                                action: function (dialogItself) {
                                    dialogItself.close();
                                }
                            }]
                        });
                    }
                });
            }
        });
    }

    $(document).ready(function () {
        //表格
        var table = $("#admingrid");
        //表格开始
        table.bootstrapTable({
            //可有可无     toolbar: "#toolbar",
            showRefresh: true,
            showColumns: true,
            showFooter: true,
            method: "post",
            cache: "false",
            search: true,
            pageSize: 3,
            pageList: [10, 20, 30],
            url: "@Url.Action("ListJson")" ,
            columns: [
                { title: "state", checkbox: true },
                { title: "ID", field: "AdministratorID" },
                { title: "账号", field: "Accounts" },
                { title:"登陆时间", field:"LoginTime",formatter: function(value) { return moment(value).format("YYYY-MM-DD HH:mm:ss")} },
                { title: "登录IP", field: "LoginIP" },
                { title: "创建时间", field: "CreateTime", formatter: function (value) { return moment(value).format("YYYY-MM-DD HH:mm:ss") } },
                { title: "操作", field: "AdministratorID", formatter: function (value, row, index) { return "<a href=\"javascript:void(0)\" onclick=\"ResetPassword(" + value + ",'" + row.Accounts + "')\">重置密码</a>" } }
            ]
        });
        //表格结束

        //工具栏
        //按钮
        $("#btnAdd").click(function () {
            var addDialog = new BootstrapDialog({
                title: "<span class = 'glyphicon glyphicon-plus'></span> 添加管理员",
                message: function (dialog) {
                    var $message = $("<div></div>");
                    var pageToLoad = dialog.getData('pageToLoad');
                    $message.load(pageToLoad);

                    return $message;
                },
                data: {
                    'pageToLoad': '@Url.Action("AddAdmin")'
                },
                buttons: [{
                    icon: "glyphicon glyphicon-saved",
                    label: "确定",
                    action: function (dialogItself) {
                        $.post($("form").attr("action"), $("form").serializeArray(), function (data) {
                            if (data.Code == 1) {
                                BootstrapDialog.show({
                                    message: data.Message,
                                    buttons: [{
                                        icon: "glyphicon glyphicon-ok",
                                        label: "确定",
                                        action: function (dialogRef) {
                                            table.bootstrapTable("refresh");
                                            dialogRef.close();
                                            addDialog.close();
                                        }
                                    }]

                                });
                            }
                            else BootstrapDialog.alert(data.Message);
                        }, "json");
                        $("form").validate();
                    }
                }, {
                    icon: "glyphicon glyphicon-remove",
                    label: "关闭",
                    action: function (dialogItself) {
                        dialogItself.close();
                    }
                }]
            });
            addDialog.open();
        });
        //按钮结束

        //工具栏
        //删除按钮
        $("#btnDel").click(function () {
            var selecteds = table.bootstrapTable('getSelections');
       
            if (selecteds.length > 0) {
                BootstrapDialog.confirm("确定要删除选中的" + selecteds.length + "位管理员?", function (result) {
                    if (result) {
                        var ids = new Array(selecteds.length);
                        $.each(selecteds, function (index, value) {
                            ids[index] = value.AdministratorID;
                        });

                        $.ajax({
                            url: "@Url.Action("DeleteJson", "Admin")",
                            data: { idList: ids },
                            type: "Post",
                            dataType: "Json",
                            traditional: true,
                            success: function (data) {
                                if (data.Code != 0) {
                                    BootstrapDialog.show({
                                        message: data.Message,
                                        buttons: [{
                                            inco: "glyphicon glyphicon-ok",
                                            label: "确定",
                                            action: function (dialogItself) {
                                                table.bootstrapTable("refresh");
                                                dialogItself.close();
                                               
                                            }
                                        }]
                                    });
                                }
                                else {
                                    BootstrapDialog.warning(data.Message);
                                }
                            }
                        });             
                    }
                });
            }
            else {
                BootstrapDialog.alert("请选中要删除的行");
            }
        });
        //删除按钮结束
    });
</script>
